﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.AdminCP
{
    public partial class MenuAdmin : System.Web.UI.Page
    {
        MenuAdminsDO objMenu = new MenuAdminsDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["MenuID"]))
            {
                lblAction.Text = "Sửa menu";
                int menuID = int.Parse(Request.QueryString["MenuID"]);
                objMenu.MenuAdminID = menuID;
            }
            else
                lblAction.Text = "Thêm menu";
            if (!IsPostBack)
            {
                //bindToDropDown(ddlMenu);
                InitForm();
                Functions.ddlDatabinder(ddlMenu,MenuAdminsDO.MENUADMINID_FIELD, MenuAdminsDO.NAME_FIELD, new MenuAdminsBL().SelectAll());
            }
        }
        protected void InitObject()
        {
            objMenu = new MenuAdminsBL().Select(objMenu);
            objMenu.Name = txtTitle.Text.Trim();
            objMenu.Order = int.Parse(txtOrder.Text.Trim());
            objMenu.Link = txtLink.Text; //txtLink.Text.Trim();
            objMenu.ParentID = int.Parse(ddlMenu.SelectedValue);

            if (chkHienthi.Checked)
                objMenu.UserType = 1;
            else
                objMenu.UserType = 0;
            
            
            int parent = int.Parse(ddlMenu.SelectedValue);
            
            MenuAdminsDO objMenux = new MenuAdminsBL().Select(new MenuAdminsDO() { MenuAdminID = parent });
            string Level = objMenux.Level;

        }
        protected void InitForm()
        {
            //objMenu.MenuAdminID =int.Parse( Request.QueryString["MenuID"].ToString());
            objMenu = new MenuAdminsBL().Select(objMenu);
            txtTitle.Text = objMenu.Name;
            //txtTitle.ReadOnly = true;
            txtOrder.Text = objMenu.Order.ToString();
            if (objMenu.UserType == 1)
                chkHienthi.Checked = true;
            else
                chkHienthi.Checked = false;
            txtLink.Text = objMenu.Link;
            //txtLink.ReadOnly = true;
            txtLink.Text = objMenu.Link;
            //txtLink.ReadOnly = true;
            try
            {
                ddlMenu.SelectedValue = objMenu.ParentID.ToString();
            }
            catch
            {
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            InitObject();
            if (objMenu.MenuAdminID > 0)
            {
                new MenuAdminsBL().Update(objMenu);
                Functions.Alert("Cập nhật thành công!", "ListMenuAdmin.aspx");
            }
            else
            {
                new MenuAdminsBL().Insert(objMenu);
                Functions.Alert("Thêm mới thành công!", "ListMenuAdmin.aspx");
            }
           
        }
        //public void bindToDropDown(DropDownList ddl)
        //{
        //    DataTable dtCate = new MenuAdminsBL().SelectAll();
        //    foreach (DataRow dr in dtCate.Rows)
        //    {
        //        if (dr["ParentID"].ToString().Equals("0"))
        //        {
        //            ListItem item = new ListItem();
        //            item.Value = dr["MenuAdminID"].ToString();
        //            item.Text = "+ " + dr["Title"].ToString();
        //            ddl.Items.Add(item);
        //            GetChildItems(dr["MenuAdminID"].ToString(), dtCate, ddl);
        //        }
        //    }
        //}
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = "-------" + dr["Title"].ToString();
                    listChild.Value = dr["MenuAdminID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                    //       GetChildItems(dr["MenuID"].ToString(), dtTemp, ddl);
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminCP/ListMenuAdmin.aspx");
        }

    }
}